# _*_ coding: utf-8

import logging
import os
import time


class Logger:
    def __init__(self, logs_dir):
        self.logger = logging.getLogger('')
        # 设置输出的等级
        LEVELS = {
            'NOTSET': logging.NOTSET,
            'DEBUG': logging.DEBUG,
            'INFO': logging.INFO,
            'WARRING': logging.WARNING,
            'ERROR': logging.ERROR,
            'CRITICAL': logging.CRITICAL
        }
        # 创建文件目录
        self.logs_dir = logs_dir
        if os.path.exists(self.logs_dir) and os.path.isdir(self.logs_dir):
            pass
        else:
            os.mkdir(self.logs_dir)
        # 修改log保存位置
        timestamp = time.strftime('%Y-%m-%d', time.localtime())
        log_filename = '{}.log'.format(timestamp)
        log_filepath = os.path.join(self.logs_dir, log_filename)
        rotatingFileHandler = logging.FileHandler(filename=log_filepath)
        # 设置输出格式
        formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')
        rotatingFileHandler.setFormatter(formatter)
        # 控制台句柄
        console = logging.StreamHandler()
        console.setLevel(LEVELS['INFO'])
        console.setFormatter(formatter)
        # 添加内容到日志句柄中
        self.logger.addHandler(rotatingFileHandler)
        self.logger.addHandler(console)
        self.logger.setLevel(LEVELS['INFO'])

    def info(self, message):
        self.logger.info(message)

    def debug(self, message):
        self.logger.debug(message)

    def warning(self, message):
        self.logger.warning(message)

    def error(self, message):
        self.logger.error(message)


if __name__ == '__main__':
    my_logger = Logger(r'C:\Users\杨梦龙\Desktop\日志')
    my_logger.info('123')
